/*
 * Copyright (c) 2010 MoSync AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <ma.h>
#include <MAInterpreter/TinyJSInterpreter.h>

using namespace MAInterpreter;

// Example TinyJS scripts.
// The really cool thing to do is to download scripts dynamically
// over the Internet. Will update this program to do that.

// Very simple painting program that draws rectangles on touch events.
#define PAINTING_PROGAM " \
function OnInit() { \
  SetColor(255, 255, 255); \
  FillRect(0, 0, ScreenWidth(), ScreenHeight()); \
} \
\
function OnTouchDown(x, y) { \
  SetColor(0, 0, 0); \
  FillRect(x - 5, y - 5, 10, 10); \
} \
\
function OnTouchDrag(x, y) { \
  OnTouchDown(x, y); \
} \
"

// Painting program that uses clocktick and draw events
// to erase the painting as you draw it.
#define WIPING_PAINTING_PROGAM " \
function OnInit() { \
  WiperY = 0; \
  SetColor(255, 255, 255); \
  FillRect(0, 0, ScreenWidth(), ScreenHeight()); \
  SetClockInterval(100); \
} \
\
function OnTouchDown(x, y) { \
  SetColor(0, 0, 0); \
  FillRect(x - 10, y - 10, 20, 20); \
} \
\
function OnTouchDrag(x, y) { \
  OnTouchDown(x, y); \
} \
\
function OnClockTick() { \
  WiperY = WiperY + 2; \
  if (WiperY > ScreenHeight()) { \
    WiperY = 0; \
  } \
} \
\
function OnDraw() { \
  SetColor(255, 255, 255); \
  FillRect(0, WiperY, ScreenWidth(), 2); \
} \
"

#include <vector>
#include <conprint.h>

/**
 * Main entry point.
 */
extern "C" int MAMain()
{
	TinyJSInterpreter interpreter;

	if (!interpreter.initialize()) { return -1; }

	// Tests used to track down a bug
	//interpreter.runScript("if (foo !== undefined) print('Not printed');"); // Works
	//interpreter.runScript("if (foo !== undefined) { foo(); }"); // Works
	//interpreter.runScript("if (foo !== undefined) foo();"); // Attempts to call foo

	interpreter.runScript(PAINTING_PROGAM);
	//interpreter.runScript(WIPING_PAINTING_PROGAM);

	interpreter.mainLoop();

	return 0;
}
